package com.ellwood.copyright.controller.system;

import cn.hutool.crypto.SecureUtil;
import com.ellwood.copyright.common.core.base.ControllerFactory;
import com.ellwood.copyright.common.core.enums.HttpCodeEnum;
import com.ellwood.copyright.common.core.result.EllPageResult;
import com.ellwood.copyright.common.core.result.EllResult;
import com.ellwood.copyright.common.core.utils.EllAESUtil;
import com.ellwood.copyright.infrastructure.system.entity.SysUserEntity;
import com.ellwood.copyright.infrastructure.system.query.SysUserQuery;
import com.ellwood.copyright.infrastructure.system.vo.SysUserDataVO;
import com.ellwood.copyright.modules.system.convert.SysUserConvert;
import com.ellwood.copyright.modules.system.service.SysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.ParseException;
import java.util.List;

/**
 * @ProjectName: ell-copyright-pro
 * @PackageName: com.ellwood.copyright.controller.system
 * @ClassName: SysUserController
 * @Description: // 用户控制器
 * @Author: Ellwood
 * @CreateDate: 2024/11/18 - 21:20
 * @Version: V1.0.0
 */
@Slf4j
@Tag(name = "用户管理", description = "用户管理")
@RestController
@RequestMapping("/api/sys/user")
@AllArgsConstructor
public class SysUserController extends ControllerFactory<SysUserEntity> {

    private final SysUserService userService;

    @Operation(summary = "查询所有")
    @GetMapping("/page")
    public EllResult<EllPageResult<SysUserDataVO>> findAll(@ParameterObject @Valid SysUserQuery userQuery) {
        EllPageResult<SysUserDataVO> userPage= userService.findAll(userQuery);
        return EllResult.ok(userPage);
    }

    @Operation(summary = "根据id查询")
    @GetMapping("{id}")
    public EllResult<SysUserDataVO> getById(@PathVariable("id") Long userId){
        SysUserEntity user = userService.getById(userId);
        SysUserDataVO vo =SysUserConvert.INSTANCE.convert(user);
        return EllResult.ok(vo);
    }

    @PostMapping
    @Operation(summary = "新增用户")
    public EllResult<String> userSave(@RequestBody @Valid SysUserDataVO userVo){
        // 先校验密码是否为空，如果为空就抛出异常，
        // 密码加密 TODO 后续加入权限控制再进行这里的处理

        // 新增用户
        int result=userService.userSave(userVo);
        return result>0?
                EllResult.ok(HttpCodeEnum.SUCCESS.getMessage())
                :EllResult.error(HttpCodeEnum.ERROR.getMessage());
    }

    @PutMapping("edit")
    @Operation(summary = "更新用户")
    public EllResult<String> editUser(@RequestBody @Valid SysUserDataVO userVo){
        // 密码修改不在此进行处理，所以
        // 修改用户
        int result= userService.editUser(userVo);
        return result>0?
                EllResult.ok(HttpCodeEnum.SUCCESS.getMessage())
                :EllResult.error(HttpCodeEnum.ERROR.getMessage());
    }

    @Operation(summary = "删除")
    @DeleteMapping
    public EllResult<String> remove(@RequestBody List<Long> ids){
        // 加入权限后获取当前登录用户不允许删除

        userService.removeByIds(ids);
        return EllResult.ok(HttpCodeEnum.SUCCESS.getMessage());
    }

    @Operation(summary = "导入用户数据")
    @PostMapping("/import")
    public EllResult<String> importByUsers(@RequestParam("file")MultipartFile file){
        try{
            userService.importByUsers(file);
            return EllResult.ok(HttpCodeEnum.data_import_success.getMessage());
        }catch (Exception e){
            log.error("导入用户数据失败：{}", e.getMessage());
            // 发生异常事返回错误响应
            return EllResult.error(HttpCodeEnum.data_import_failed.getMessage());
        }
    }

    @Operation(summary ="导出用户数据")
    @GetMapping("/export")
    public EllResult<String> exportByUsers(@RequestParam
                                           @Parameter(description = "导出文件存放路径") String filePath,
                                           @Parameter(description = "导出文件名称") String fileName
    ){
        try{
            userService.exportByUsers(filePath, fileName);
            return EllResult.ok(HttpCodeEnum.data_export_success.getMessage());
        }catch (Exception e){
            log.error("导出用户数据失败：{}", e.getMessage());
            // 发生异常事返回错误响应
            return EllResult.error(HttpCodeEnum.data_export_failed.getMessage());
        }
    }

}
